#####################多级目录源文件生成一个库和测试文件

## cmake版本需求
cmake_minimum_required(VERSION 3.5)

## 工程名称
project(calculate)


## 添加头文件包含路径，相当于makefile中的-I
include_directories(.)
include_directories(add/include/)
include_directories(sub/include/)


## 设置add sub 头文件和源文件列表
set(ADD_HEADERS add/include/add.h)
set(ADD_SOURCES add/src/add.c)
set(SUB_HEADERS sub/include/sub.h)
set(SUB_SOURCES sub/src/sub.c)

########方法一：先将add和sub都生成.o文件然后生成calculate库时链接.o文件###################
add_library(add OBJECT ${ADD_SOURCES} ${ADD_HEADERS}) ##相当于makefile中的 gcc -c add.c -o add.o -I./add/include
add_library(sub OBJECT ${SUB_SOURCES} ${SUB_HEADERS})
set(CALCULATE_LIBRARIES_OBJECTS $<TARGET_OBJECTS:add> $<TARGET_OBJECTS:sub>) # 设置calculate静态库生成所需的依赖库(目标文件.o) STATIC可省略
add_library(calculate STATIC ${CALCULATE_LIBRARIES_OBJECTS})
#########################################################################################


########方法二：直接将add和sub的头文件和源文件添加到生成calculate库所需要的文件，不生成中间文件.o###################
#add_library(calculate STATIC ${SUB_HEADERS} ${SUB_SOURCES} ${ADD_SOURCES} ${ADD_HEADERS})
####################################################################################################################


## message打印消息
message("project name:${PROJECT_NAME}")
message("project source name:${PROJECT_SOURCE_DIR}")
message("project binary name:${PROJECT_BINARY_DIR}")


## 设置目标输出名称
#set_target_properties(calculate PROPERTIES OUTPUT_NAME "calculate")
set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "calculate")


## 设置目标库的前缀为lib
#set_target_properties(calculate PROPERTIES PREFIX "lib")
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "lib")


## 添加test_main可执行文件，并且链接到libcalculate库
set(APP_SRC test_main.c)
add_executable(test_main ${APP_SRC})
target_link_libraries(test_main calculate)
